include('shared.lua')

function ENT:HUDPaint(tr)
	if (tr.Entity == self && self:GetPos():Distance(LocalPlayer():GetPos()) < 256) then
		local pos = self:GetPos()
		pos.z = pos.z + 14
		pos = pos:ToScreen()
		
		local power = self.dt.Power
		local upgrade = self.dt.Upgrade or 0
		
		draw.RoundedBox(0, pos.x-100, pos.y-50, 200, 100, Color(50, 50, 50, 200))
		//draw.RoundedBox(0,  pos.x-101, pos.y-51, 198, 98, Color(0, 0, 0, 200))
		
		draw.RoundedBox(0, pos.x - 100, pos.y - 28, 200, 1, Color(50, 50, 50, 200)) //name separator top
		draw.RoundedBox(0, pos.x - 100, pos.y + 27, 200, 1, Color(50, 50, 50, 200)) //name separator bottom
		if upgrade == 0 then
			upgradestring = ""
		elseif upgrade == 1 then
			upgradestring = "I"
		elseif upgrade == 2 then
			upgradestring = "II"
		elseif upgrade == 3 then
			upgradestring = "III"
		elseif upgrade == 4 then
			upgradestring = "IV"
		end
		
		draw.DrawText(upgradestring, "Default", pos.x + 90, pos.y - 46, Color(200, 200, 0, 255), 1)
		
		local t = {}
		t.text		= self.PrintName
		t.pos		= {pos.x - 10, pos.y - 50}
		t.font		= "TargetID"
		t.color		= Color(100, 150, 200, 255)
		t.xalign	= TEXT_ALIGN_CENTER
		draw.TextShadow(t, 1, 255)
	
		if self.Power > 0 && self:GetClass() != "struct_powerplant" then
			t.text 	= "Z"
			t.font 	= "HL2Symbols"
			t.color = Color(0, 255, 0, 255)
			
			for i = 1, power do
				t.pos = {pos.x - 89 + (i * 30), pos.y - 74}
				draw.TextShadow(t, 1, 255)
			end
			
			if self.dt.Power < self.Power then
				t.text 	= "Low power."
				t.font	= "TargetID"
				t.color	= Color(150, 0, 0, 255)
				t.pos	= {pos.x - 10, pos.y + 5}
				draw.TextShadow(t, 1, 255)
			end
		end
		
		if self:GetClass() == "struct_powerplant" then	
			t.text 	= "Z"
			t.font 	= "HL2Symbols"
			local colori = 0
				for i = 1, 5 do
					colori = colori + 1
					if(colori <= power) then 
						t.color = Color(0, 255, 0, 255)
					else
						t.color = Color(255, 0, 0, 255)
					end
					t.pos = {pos.x - 89 + (i * 30), pos.y - 74}
					draw.TextShadow(t, 1, 255)
				end
		end
		if(ValidEntity(self.dt.Owner)) then
			t.text	= self.dt.Owner:Name()
		else
			t.text  = "World"
		end
		t.pos	= {pos.x, pos.y + 25}
		t.font	= "TargetID"
		t.color	= Color(100, 100, 100, 255)
		draw.TextShadow(t, 1, 255)
	end
end

function ENT:Think()
	if tonumber(LocalPlayer():GetInfo("rpdm_showsparks"))==1 then
		if self.dospark==nil then self.dospark=0 end
		if self.dt.Sparking && self.SparkPos!=nil && self.dospark==0 then
			self.dospark=self.dospark+1
			local ang = self.Entity:GetAngles()
			local spos = self.SparkPos
			local effectdata = EffectData()
				effectdata:SetOrigin(self.Entity:GetPos()+ang:Forward()*spos.x+ang:Right()*spos.y+ang:Up()*spos.z)
				effectdata:SetMagnitude(1)
				effectdata:SetScale(1)
				effectdata:SetRadius(2)
			util.Effect("Sparks", effectdata)
		else
			self.dospark=self.dospark+1
			if self.dospark==3 then self.dospark=0 end
		end
		self.Entity:NextThink(CurTime()+1)
		return true
	end
	self.Entity:NextThink(CurTime()+2)
	return true
end

local sprite = Material("sprites/light_glow02_add")
function ENT:Draw(bDontDrawModel)

	if (LocalPlayer():GetEyeTrace().Entity == self.Entity && 
	     EyePos():Distance(self.Entity:GetPos()) < 256) then
	
		if (self.RenderGroup == RENDERGROUP_OPAQUE) then
			self.OldRenderGroup = self.RenderGroup
			self.RenderGroup = RENDERGROUP_TRANSLUCENT
		end

		if (self:GetOverlayText() != "") then
			AddWorldTip(self.Entity:EntIndex(), self:GetOverlayText(), 0.5, self.Entity:GetPos(), self.Entity )
		end

	else
	
		if (self.OldRenderGroup != nil) then
		
			self.RenderGroup = self.OldRenderGroup
			self.OldRenderGroup = nil
		
		end
	
	end

	if (!bDontDrawModel) then self:DrawModel() end
	
	if self.SparkPos!=nil && tonumber(LocalPlayer():GetInfo("rpdm_showsparks"))==0 then
		local ang = self.Entity:GetAngles()
		local spos = self.SparkPos
		local dotpos = self.Entity:GetPos()+ang:Forward()*spos.x+ang:Right()*spos.y+ang:Up()*spos.z
		local dotcolor = Color(200,200,200,200)
		if self.Entity:GetNWBool("sparking") then
			dotcolor = Color(255,100,100,200)
		end
		render.SetMaterial(sprite)
		render.DrawSprite(dotpos,10,10,dotcolor)
	end
end

local matOutlineWhite 	= Material("white_outline")
local ScaleNormal		= Vector()
local ScaleOutline1		= Vector() * 1.05
local ScaleOutline2		= Vector() * 1.1
local matOutlineBlack 	= Material("black_outline")

function ENT:DrawEntityOutline(size)
	
	size = size or 1.0
	render.SuppressEngineLighting(true)
	render.SetAmbientLight(1, 1, 1)
	render.SetColorModulation(1, 1, 1)
	
		// First Outline	
		self:SetModelScale(ScaleOutline2 * size)
		SetMaterialOverride(matOutlineBlack)
		self:DrawModel()
		
		
		// Second Outline
		self:SetModelScale(ScaleOutline1 * size)
		SetMaterialOverride(matOutlineWhite)
		self:DrawModel()
		
		// Revert everything back to how it should be
		SetMaterialOverride(nil)
		self:SetModelScale(ScaleNormal)
		
	render.SuppressEngineLighting(false)
	
	local r, g, b = self:GetColor()
	render.SetColorModulation(r/255, g/255, b/255)
	
	
	local width = self.HealthRing[2]
	if width==nil then width = 1 end
	local eang = self.Entity:GetAngles()
	local rot = Vector(0, 90, 0)
	if self.HealthAxis then rot = Vector(90,90,0) end
	local offsetright = Vector(0,0,0)
	if self.HealthOffset!=nil then offsetright=self.Entity:GetAngles():Right()*self.HealthOffset end
	local pos = self.Entity:GetPos()+self.Entity:GetAngles():Up()*self.HealthRing[3]+offsetright
	eang:RotateAroundAxis(eang:Right(), rot.x)
	eang:RotateAroundAxis(eang:Up(), rot.y)
	eang:RotateAroundAxis(eang:Forward(), rot.z)
	
	cam.Start3D2D(pos, eang, 1)
		local health = 16*math.Round(22.5*(self.dt.Health/self.HealthRing[1]))
		if health<=90 then
			surface.SetDrawColor(255,0,0,200)
		elseif health<=180 then
			surface.SetDrawColor(255,255,0,200)
		else
			surface.SetDrawColor(0,255,0,200)
		end
		local innerwidth = width*.5
		surface.SetTexture(whitetexture)
		for i=0,health,22.5 do
			local a = i-20.5
			local o = i-21.5
			local u = i-1
			local e = i-2
			
			local tbl = {}
			tbl[1]={}
			tbl[2]={}
			tbl[3]={}
			tbl[4]={}
			
			tbl[1]["x"]=math.sin(math.rad(u))*width
			tbl[1]["y"]=math.cos(math.rad(u))*width
			tbl[2]["x"]=math.sin(math.rad(e))*innerwidth
			tbl[2]["y"]=math.cos(math.rad(e))*innerwidth
			tbl[3]["x"]=math.sin(math.rad(a))*innerwidth
			tbl[3]["y"]=math.cos(math.rad(a))*innerwidth
			tbl[4]["x"]=math.sin(math.rad(o))*width
			tbl[4]["y"]=math.cos(math.rad(o))*width
			surface.DrawPoly(tbl)
		end
		local iwidth = width+1
		local xpos = 0
		local ypos = iwidth
		surface.SetDrawColor(0,200,200,200)
		for i=1,16,1 do
			local throwawayx = math.sin(math.rad(i*22.5))*iwidth
			local throwawayy = math.cos(math.rad(i*22.5))*iwidth
			surface.DrawLine(xpos,ypos, throwawayx,throwawayy)
			xpos=throwawayx
			ypos=throwawayy
		end
	cam.End3D2D()
	
end
